<style>
.modal_giftbox{ padding:20px;}
.modal_giftbox div[class^=col-]{ line-height:30px}
.modal_giftboxsecrchinput{vertical-align:-10px;border:1px solid #c4c4c4;position:relative;overflow:hidden;border-radius:15px;height:30px;margin-right:8px; background:#FFF; width:80%; float:left; line-height:20px; margin-bottom:10px;}
.modal_giftboxsecrchinput input{border:none; width:80%;}
.modal_giftboxsecrchinput button{background:none; border:none; padding:5px 8px;color:#5FB878}

</style>
<div class="modal_gift" style="display:none">
  <div class="modal_giftbox">
  	<div class="row">
    	<div class="col-sm-4">
        	<div class="row">
            	<div class="col-sm-12">
                  <div class="modal_giftboxsecrchinput">
                    <form action="" method="post" class="layui-form" id="modal_giftform" onsubmit="return false;" enctype="multipart/form-data">
                      <button type="submit" onclick="funExtend_giftGetmember();"><i class="fa fa-search"></i></button>
                      <input type="text" name="modal_gift_txtTopupSeachStr" id="modal_gift_txtTopupSeachStr" placeholder="输入手机号码/卡号">
                    </form>
                  </div>
                </div>
            </div>
        	<div class="row">
            	<div class="col-sm-12">卡号：<span id="mdl_cardno"></span><input type="hidden" name="mdl_id" id="mdl_id"/></div>
            </div>
            <div class="row">
            	<div class="col-sm-6">姓名：<span id="mdl_realname"></span></div>
                <div class="col-sm-6">电话：<span id="mdl_mobile"></span></div>
            </div>
            <div class="row">
            	<div class="col-sm-6">积分：<span id="mdl_credit"></span></div>
                <div class="col-sm-6">等级：<span id="mdl_level"></span></div>
            </div>
        </div>
        <div class="col-sm-8">
        	<div class="layui-form" style="height:290px; overflow-y:scroll; padding-right:10px">
              <table class="layui-table" lay-skin="line">
                <thead>
                  <tr>
                    <th>编号</th>
                    <th>名称</th>
                    <th>类型</th>
                    <th>积分</th>
                    <th>数量</th>
                    <th>操作</th>
                  </tr>
                </thead>
                <tbody id="giftlist">
                	
                </tbody>
              </table>
            </div>
        </div>
    </div>
    <div class="layui-form" style="height:290px; overflow-y:scroll; padding-right:10px">
      <table class="layui-table" lay-skin="line">
        <thead>
          <tr>
            <th>卡号</th>
            <th>姓名</th>
            <th>礼品</th>
            <th>使用积分</th>
            <th>操作人</th>
            <th>日期</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody id="membergiftlist">
            
        </tbody>
      </table>
    </div>
    <div id="giftlistpageindex"></div>
  </div>
</div>
<script>
function funExtend_gift(){
	var _height=$(window).height()-30;
	var _width=$(window).width()*0.8;
	layer.open({
		type: 1,
		title:"兑换礼物",
		area: _width+'px',
		skin: 'layui-layer-rim',
		content:$(".modal_gift").show(),
		success: function(layero, index){
			$("#modal_giftform")[0].reset();
			funExtend_getGift();
			funExtend_giftGetRecord(1);
		},
		end:function(){
			$(".modal_gift").hide();
		}
	});
}
function funExtend_getGift(){
	var index2=layer.load(1, {shade: [0.1,'#fff']});
	$.post("{php echo $this->createMobileUrl('index',array('op'=>'getgiftlist'))}",'',function(returnstr){
		layer.close(index2);
		console.log(returnstr);
		var result=eval("("+returnstr+")");
		if(!result.success){
			layer.msg(result.msg,{icon:2,time:1000});
			return false;
		}
		var list=result.list;
		var gifttype=['实物','卡券','红包','余额',];
		$("#giftlist").empty();
		for(var i in list){
            var temp='<Tr><td>'+list[i].goodsn+'</td><td>'+list[i].title+'</td><td>'+gifttype[list[i].gtype]+'</td><td>'+list[i].credit+'</td><td> '+parseInt(list[i].num ? list[i].num : "0")+'/'+list[i].remain+'/'+list[i].store+'</td><td><button type="button" onclick="funExtend_giftselect('+list[i].id+')" class="layui-btn layui-btn-primary layui-btn-mini">兑换</button></td></tr>';
			$("#giftlist").append(temp);
		}
		
	});
}
function funExtend_giftGetmember(){
	if(!$("#modal_gift_txtTopupSeachStr").val())return false;;
	var keyword=$("#modal_gift_txtTopupSeachStr").val();
	if(!keyword){
		layer.msg("请输入卡号或者电话号码",{icon:2,time:2000},function(){$("#modal_gift_txtTopupSeachStr").focus()});
		return false;
	}
	var index2=layer.load(1, {shade: [0.1,'#fff']});
	$.post("{php echo $this->createMobileUrl('index',array('op'=>'getmember'))}",{keyword:keyword},function(returnstr){
		layer.close(index2);
		console.log(returnstr);
		var result=eval("("+returnstr+")");
		if(!result.success){
			layer.msg(result.msg,{icon:2,time:1000});
			return;
		}
		var member=result.member;
		var cost=result.cost;
		$("#mdl_id").val(member.id);
		$("#mdl_cardno").text(member.cardno);
		$("#mdl_realname").text(member.realname);
		$("#mdl_mobile").text(member.mobile);
		$("#mdl_credit").text(member.credit2);
		$("#mdl_level").text(member.level);
		$("#mdl_cash").text(member.cash2);
	});
}
function funExtend_giftGetRecord(pindex){
	var keyword=$("#mdl_id").val();
	var index2=layer.load(1, {shade: [0.1,'#fff']});
	$.post("{php echo $this->createMobileUrl('index',array('op'=>'getgiftrecord'))}",{keyword:keyword,page:pindex},function(returnstr){
		layer.close(index2);
		console.log(returnstr);
		var result=eval("("+returnstr+")");
		if(!result.success){
			layer.msg(result.msg,{icon:2,time:1000});
			return;
		}
		if(pindex==1){
			layui.use(['laypage'], function(){
				var laypage = layui.laypage;
				laypage.render({
					elem: 'giftlistpageindex'
					,count: result.total
					,jump: function(obj, first){
						var curr = obj.curr;
						if(!first){
							funExtend_giftGetRecord(curr);
							return;
						}
					}
				});
			});
		}
		var list=result.list;
		var temp="";
		for(var i in list){
			temp+='<tr><td>'+list[i].cardno+'</td><td>'+list[i].realname+'</td><td>'+list[i].title+'</td><td>'+list[i].credit+'</td><td>'+list[i].username+'</td><td>'+fromatTime(list[i].createtime)+'</td>';
			if(parseInt(list[i].status)){
				temp+='<td><span class="label label-success">成功</span></td>';
			}else{
				temp+='<td><span class="label label-danger">失败</span> <button type="button" class="layui-btn layui-btn-small" onclick="funExtend_giftsend('+list[i].id+')">补发</button></td>';
			}
			temp+='</tr>';
		}
		$("#membergiftlist").html(temp);
	});
}
function funExtend_giftsend(id){
	var index2=layer.load(1, {shade: [0.1,'#fff']});
	$.post("{php echo $this->createMobileUrl('index',array('op'=>'sendgift'))}",{id:id},function(returnstr){
		layer.close(index2);
		console.log(returnstr);
		var result=eval("("+returnstr+")");
        if(result.success){
            layer.msg("操作成功",{icon:1,time:2000},function(){
                if(result.out_trade_no)fun_print(12,result.out_trade_no);
                funExtend_getGift();
                funExtend_giftGetmember();
                funExtend_giftGetRecord(1);
            });
        }else{
            layer.msg(result.msg,{icon:2,time:1000});
            return;
        }
	})
}
function funExtend_giftselect(id){
	var memeberid=$("#mdl_id").val();
	if(!memeberid){
		layer.msg("请先选择会员",{icon:2,time:1000});
		return;
	}
	layer.confirm('确认兑换此礼品？', {
		btn: ['确认','取消']
	}, function(){
		
		var index2=layer.load(1, {shade: [0.1,'#fff']});
		$.post("{php echo $this->createMobileUrl('index',array('op'=>'exchangegift'))}",{id:id,mid:memeberid},function(returnstr){
			layer.close(index2);
			console.log(returnstr);
			var result=eval("("+returnstr+")");
            if(result.success){
                layer.msg("兑换成功",{icon:1,time:2000},function(){
                    fun_print(12,result.out_trade_no);
                    funExtend_getGift();
                    funExtend_giftGetmember();
                    funExtend_giftGetRecord(1);
                });
            }else{
                layer.msg(result.msg,{icon:2,time:1000});
                return;
            }
		})
	});
}
</script>